﻿Imports System.Data.SqlClient
Imports System.IO

Public Class Employees
    Public Shared Sub AddEmployee(ByVal emp As EmployeeInfo)
        CmdPro.Begin()
        Try

            CmdPro.Parameters("@empCode") = emp.EmployeeCode
            CmdPro.Parameters("@englishName") = emp.EnglishName
            CmdPro.Parameters("@khmerName") = emp.KhmerName
            CmdPro.Parameters("@sex") = emp.Sex
            CmdPro.Parameters("@DOB") = emp.DOB
            CmdPro.Parameters("@provinceId") = emp.ProvinceId
            CmdPro.Parameters("@address") = emp.Address
            CmdPro.Parameters("@status") = emp.MaritalStatus
            CmdPro.Parameters("@IdCard") = emp.IdCard
            CmdPro.Parameters("@photo") = emp.photo
            CmdPro.Parameters("@phone") = emp.Phone
            CmdPro.Parameters("@email") = emp.Email
            CmdPro.Parameters("@startDate") = emp.StartDate
            CmdPro.Parameters("@contract") = emp.TypeOfContract
            CmdPro.Parameters("@PositionId") = emp.PositionId
            CmdPro.Parameters("@salaryStartDate") = emp.SalaryStartDate
            CmdPro.Parameters("@groupId") = emp.GroupId
            CmdPro.Parameters("@note") = emp.Note
            CmdPro.Parameters("@userId") = emp.Selection
            CmdPro.Parameters("@blood") = emp.Blood
            CmdPro.Parameters("@active") = emp.Active
            emp.EmployeeId = CmdPro.ExecuteNonQuery("InsertEmployees", "@id")


            CmdPro.Commit()
        Catch ex As Exception
            CmdPro.Rollback()
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Shared Function GetAllEmployees() As DataTable
        Try
            Dim dataAdapter As SqlDataAdapter
            dataAdapter = New SqlDataAdapter("SELECT * FROM VIEW_EMPLOYEE WHERE ACTIVE=1 ORDER BY EMPLOYEE_CODE DESC", CType(Dbinterface.Connection, SqlConnection))
            Dim dataTable As DataTable = New DataTable()
            dataAdapter.Fill(dataTable)

            Return dataTable
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            Return Nothing
        End Try
    End Function

    Public Shared Sub UpdateEmployee(ByVal emp As EmployeeInfo)
        CmdPro.Begin()
        Try

            CmdPro.Parameters("@id") = emp.EmployeeId
            CmdPro.Parameters("@empCode") = emp.EmployeeCode
            CmdPro.Parameters("@englishName") = emp.EnglishName
            CmdPro.Parameters("@khmerName") = emp.KhmerName
            CmdPro.Parameters("@sex") = emp.Sex
            CmdPro.Parameters("@DOB") = emp.DOB
            CmdPro.Parameters("@provinceId") = emp.ProvinceId
            CmdPro.Parameters("@address") = emp.Address
            CmdPro.Parameters("@status") = emp.MaritalStatus
            CmdPro.Parameters("@IdCard") = emp.IdCard
            CmdPro.Parameters("@photo") = emp.Photo
            CmdPro.Parameters("@phone") = emp.Phone
            CmdPro.Parameters("@email") = emp.Email
            CmdPro.Parameters("@startDate") = emp.StartDate
            CmdPro.Parameters("@contract") = emp.TypeOfContract
            CmdPro.Parameters("@PositionId") = emp.PositionId
            CmdPro.Parameters("@salaryStartDate") = emp.SalaryStartDate
            CmdPro.Parameters("@groupId") = emp.GroupId
            CmdPro.Parameters("@note") = emp.Note
            CmdPro.Parameters("@userId") = emp.Selection
            CmdPro.Parameters("@blood") = emp.Blood
            CmdPro.Parameters("@active") = emp.Active
            emp.EmployeeId = CmdPro.ExecuteNonQuery("UpdateEmployees")


            CmdPro.Commit()
        Catch ex As Exception
            CmdPro.Rollback()
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Shared Sub DeleteEmployee(ByVal emp As EmployeeInfo)
        CmdPro.Begin()
        Try
            CmdPro.Parameters("@id") = emp.EmployeeId
            CmdPro.ExecuteNonQuery("DeleteEmployee")

            CmdPro.Commit()
        Catch ex As Exception
            CmdPro.Rollback()
            MsgBox(ex.Message)
        End Try
    End Sub
End Class
